package com.cgw.game.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @类描述 压力状态视图
 * @作者 YF
 * @创建时间 2019/11/28 15:39
 */
public class PressureStateView extends View {
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;
    /**
     * 进度画笔
     * */
    private Paint mProPaint;
    /**
     * 刻度画笔
     * */
    private Paint mMarkPaint;

    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;

    /**
     * 进度
     * */
    private int progress = 0;
    /**
     * 最大刻度
     * */
    private int max = 100;
    /**
     * 进度宽度
     * */
    private float strokeWidth = 50f;
    /**
     * 箭头距离进度的距离
     * */
    private float space = 50f;
    private Context context;

    ValueAnimator animator;
    public PressureStateView(Context context) {
        this(context,null);
    }

    public PressureStateView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PressureStateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setColor(Color.parseColor("#00374E"));
        mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mBgPaint.setStrokeWidth(strokeWidth);

        mProPaint = new Paint();
        mProPaint.setAntiAlias(true);
        mProPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mProPaint.setStrokeWidth(strokeWidth);

        mMarkPaint = new Paint();
        mMarkPaint.setAntiAlias(true);
        mMarkPaint.setColor(Color.parseColor("#4FE5FF"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureHeightSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
        int[] colors = new int[]{Color.parseColor("#FFA500"),Color.RED,Color.YELLOW,Color.GREEN};
        LinearGradient gradient = new LinearGradient(0,-15,0,height+15,colors,null, Shader.TileMode.REPEAT);
        mProPaint.setShader(gradient);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = 300;
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }
    private int measureHeightSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = 600;
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(width/2,height);
        canvas.drawColor(Color.BLACK);
        drawBg(canvas);
        drawMark(canvas);
        drawProgress(canvas);
    }
    private void drawBg(Canvas canvas) {
        canvas.drawLine(0,-15,0,-height+15,mBgPaint);
    }

    private void drawMark(Canvas canvas) {
        float start = ((height-15)/(max*1f))*progress;
        Path pathLeft = new Path();
        pathLeft.moveTo(-space, -start);
        pathLeft.lineTo(-70-space, -start-15);
        pathLeft.lineTo(-70-space, -start+15);
        pathLeft.close();
        canvas.drawPath(pathLeft,mMarkPaint);

        Path pathRight = new Path();
        pathRight.moveTo(space, -start);
        pathRight.lineTo(70+space, -start-15);
        pathRight.lineTo(70+space, -start+15);
        pathRight.close();
        canvas.drawPath(pathRight,mMarkPaint);
    }

    private void drawProgress(Canvas canvas) {
        float start = ((height-15)/(max*1f))*progress;
        canvas.drawLine(0,-15,0,-start,mProPaint);
    }

    public void setProgress(int pro) {
        startAnimator(pro);
    }
    private void startAnimator(int pro){
        if(animator!=null&&animator.isRunning()){
            animator.cancel();
            animator = null;
        }
        animator = ValueAnimator.ofInt(pro);
        animator.setDuration(2500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                progress = (int)animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}
